Hydrix Chain — Events & Monitoring
Hydrix Chain emits events from smart contracts and provides mechanisms for developers to monitor network activity efficiently.
This section focuses on subscribing to events, indexing logs, and using lightweight nodes for high-performance monitoring.
Smart Contract Events
Events are emitted from contracts to signal state changes:
// Example Solidity contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract HelloHydrix {
string public message = "Hello from Hydrix!";
event MessageUpdated(string newMessage);
function setMessage(string calldata newMessage) external {
message = newMessage;
emit MessageUpdated(newMessage);
}
}
MessageUpdatedis emitted whenever the message changes- Events are indexed on-chain, allowing dApps to track changes efficiently
Listening to Events with Ethers.js
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider("https://chainfree.info:5010/validator/3");
const contractAddress = "DEPLOYED_CONTRACT_ADDRESS";
const abi = [
"event MessageUpdated(string newMessage)",
"function message() view returns (string)"
];
const contract = new ethers.Contract(contractAddress, abi, provider);
// Listen in real-time
contract.on("MessageUpdated", (newMessage, event) => {
console.log("Message updated:", newMessage);
console.log("Event details:", event);
});
- Events are asynchronous and will fire whenever transactions trigger them
- Useful for updating UI, triggering notifications, or logging activity
Indexing Events
For large dApps, it’s recommended to index past events for historical data:
const filter = contract.filters.MessageUpdated();
const logs = await contract.queryFilter(filter, 0, "latest");
logs.forEach(log => {
console.log("Past message update:", log.args.newMessage);
});
queryFilterallows fetching events from a specific block range- Combine with a database to maintain a historical record
Monitoring Validator & Node Health
Hydrix lightweight nodes can be used to monitor validator performance:
- Track block production times and latency
- Detect validator downtime
- Relay read operations for dApps to reduce load on main RPC endpoints
Example: Check latest block number from multiple validators:
const validators = [
"https://chainfree.info:5010/validator/1",
"https://chainfree.info:5010/validator/2",
"https://chainfree.info:5010/validator/3",
"https://chainfree.info:5010/validator/4"
];
async function checkValidators() {
for (let i = 0; i < validators.length; i++) {
try {
const provider = new ethers.JsonRpcProvider(validators[i]);
const blockNumber = await provider.getBlockNumber();
console.log(`Validator ${i + 1} latest block:`, blockNumber);
} catch (err) {
console.warn(`Validator ${i + 1} unreachable`, err);
}
}
}
checkValidators();
Best Practices
- Use event subscriptions for real-time updates
- Use queryFilter and caching for historical data to reduce RPC load
- Combine lightweight nodes and validator failover to maintain reliability
- Always test event monitoring scripts on Hydrix testnet before mainnet deployment